package com.server.config;

import com.alibaba.druid.pool.DruidDataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

import tk.mybatis.spring.annotation.MapperScan;
import tk.mybatis.spring.mapper.MapperScannerConfigurer;

/**
 * @author zhangxiaowei 2020/4/29
 */
@Configuration
@MapperScan(basePackages = "com.server.mapper", sqlSessionTemplateRef = "sqlSessionTemplate")
public class MyBatisConfig {
    private final static String BASE_PACKAGE_NAME = "com.server.mapper";

    private final static String SESSION_FACTORY_NAME = "sqlSessionFactory";

    private final static String SESSION_TEMPLATE_NAME = "sqlSessionTemplate";

    @Bean(name = "dataSource1")
    @ConfigurationProperties(prefix = "spring.datasource")
    @Primary
    public DataSource dataSource1() {
        return getDatasource();
    }

    protected DataSource getDatasource() {
        DruidDataSource dataSource = new DruidDataSource();
        return dataSource;
    }

    @Bean
    protected MapperScannerConfigurer getMapperScannerConfigurer() {
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage(getBasePackageName());
        mapperScannerConfigurer.setSqlSessionFactoryBeanName(getSessionFactoryBeanName());
        mapperScannerConfigurer.setSqlSessionTemplateBeanName(getSessionTemplateBeanName());
        return mapperScannerConfigurer;
    }

    @Bean()
    @Primary
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/db1/*.xml"));
        return bean.getObject();
    }

    @Bean()
    @Primary
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource1") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean()
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    protected String getBasePackageName() {
        return BASE_PACKAGE_NAME;
    }

    protected String getSessionFactoryBeanName() {
        return SESSION_FACTORY_NAME;
    }

    protected String getSessionTemplateBeanName() {
        return SESSION_TEMPLATE_NAME;
    }
}
